﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Testpad.BTS;

namespace BTS6000
{
    public partial class frmData : Form
    {
        public frmData()
        {
            InitializeComponent();
        }

        private void frmData_Load(object sender, EventArgs e)
        {

        }

        private void txtBarcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if((int)e.KeyChar==13)
            {
                if(Testpad.BTS.BTS6000.Instance!=null)
                {
                    int channel = 1;

                    Testpad.BTS.BTS6000.Instance.SendBarcode(channel, txtBarcode.Text);
                    txtBarcode.Clear();
                }
            }
        }

        private string GetDataByKey(List<Testpad.BTS.Result> Result,string Key)
        {
            foreach (var v in Result)
            {
                if (v.Key== Key)
                {
                    return string.Format("{0}={1},{2}", v.Name, v.Value, v.Flag);
                }
            }
            return string.Empty;
        }

        public void Display(Testpad.BTS.Data Data)
        {
            if(this.InvokeRequired)
            {
                this.Invoke(new Action<Testpad.BTS.Data>(Display), Data);
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                //Keys.xml
                sb.AppendFormat("OCV1:{0}\r\n", GetDataByKey(Data.Result, "D00"));
                sb.AppendFormat("IR:{0}\r\n", GetDataByKey(Data.Result, "D02"));
                sb.AppendFormat("R1:{0}\r\n", GetDataByKey(Data.Result, "D03"));
                sb.AppendFormat("R1T:{0}\r\n", GetDataByKey(Data.Result, "D04"));
                sb.AppendFormat("OCV2:{0}\r\n", GetDataByKey(Data.Result, "D01"));


                sb.AppendFormat("Passed?{0}\r\n", Data.AllPassed);
                sb.AppendFormat("Barcode:{0}\r\n", Data.Barcode);
                sb.AppendFormat("Channel:{0}\r\n", Data.Tag.Channel);

                txtOutput.Text = sb.ToString();
            }
        }

    }
}
